<?php
/**
 * Created by PhpStorm.
 * User: ^2_3^岑业宝
 * Date: 2018/1/11
 * Time: 15:50
 */

namespace App\Listeners;
use App\Repositories\StatusOrderRecordRepository;
use App\Repositories\StatusPayRecordRepository;
use App\Repositories\StatusShippingRecordRepository;
use Illuminate\Support\Facades\Log;

/**
 * 订阅_订单事件
 * Class EventOrderSubscriber
 * @package App\Listeners
 * @author ^2_3^岑业宝
 */
class EventOrderSubscriber
{

    /**
     * 订单状态变动记录
     * @var StatusOrderRecordRepository
     */
    protected $statusOrderRecordRepository;

    /**
     * 支付状态变动记录
     * @var StatusPayRecordRepository
     */
    protected $statusPayRecordRepository;

    /**
     * 发货状态变动记录
     * @var StatusShippingRecordRepository;
     */
    protected $statusShippingRecordRepository;

    public function __construct(
        StatusOrderRecordRepository $statusOrderRecordRepository,
        StatusPayRecordRepository $statusPayRecordRepository,
        StatusShippingRecordRepository $statusShippingRecordRepository
    ) {
        $this->statusOrderRecordRepository = $statusOrderRecordRepository;
        $this->statusPayRecordRepository = $statusPayRecordRepository;
        $this->statusShippingRecordRepository = $statusShippingRecordRepository;
    }

    /**
     * 处理订单状态事件
     * @param $event
     * @throws \Exception
     * @author ^2_3^岑业宝
     */
    public function onOrderStatus($event) {
        /// 订单模型
        $m_order = $event->orderInfo;

        /// 验证参数
        if( !isset($m_order->order_status_old) ||
            !isset($m_order->user_type) ||
            !isset($m_order->show4user)
        ) {
            $msg_err = '[订单状态事件] 提示：缺少属性；';
            $msg_err .= '使用 model->field = attribute 方式设置以下属性; order_status_old(变动前订单状态值)；';
            $msg_err .= 'user_type(操作人员类型: 0-系统默认; 1-管理人员; 2-用户)；';
            $msg_err .= 'show4user(是否显示给用户, 1-显示, 0-不显示;)；';

            throw new \Exception($msg_err, 400);
        }

        /// 订单状态
        $a_o_status = config('custom.order_status');
        $notes = "{$a_o_status[$m_order->order_status_old]}({$m_order->order_status_old})".
            " 改成 {$a_o_status[$m_order->order_status]}({$m_order->order_status});";

        /// 记录数据
        $user_id = $this->getUserid( $m_order );
        $data_so = array(
            "order_id" => $m_order->order_id,
            "old_status" => $m_order->order_status_old,
            "new_status" => $m_order->order_status,
            "notes" => $notes,
            "user_type" => $m_order->user_type,
            "user_id" => $user_id,
            "show4user" => $m_order->show4user
        );

        /// 记录_订单状态变动;
        $this->statusOrderRecordRepository->create($data_so);
    }

    /**
     * 处理支付状态事件
     * @param $event
     * @throws \Exception
     * @author ^2_3^岑业宝
     */
    public function onPayStatus($event) {
        /// 订单模型
        $m_order = $event->orderInfo;

        /// 验证参数
        if( !isset($m_order->pay_status_old) ||
            !isset($m_order->user_type) ||
            !isset($m_order->show4user)
        ) {
            $msg_err = '[支付状态事件] 提示：缺少属性；';
            $msg_err .= '使用 model->field = attribute 方式设置以下属性; pay_status_old(变动前支付状态值)；';
            $msg_err .= 'user_type(操作人员类型: 0-系统默认; 1-管理人员; 2-用户)；';
            $msg_err .= 'show4user(是否显示给用户, 1-显示, 0-不显示;)；';

            throw new \Exception($msg_err, 400);
        }

        /// 支付状态
        $a_p_status = config('custom.pay_status');
        $notes = "{$a_p_status[$m_order->pay_status_old]}({$m_order->pay_status_old})";
        $notes .= " 改成 {$a_p_status[$m_order->pay_status]}({$m_order->pay_status});";

        /// 记录数据
        $user_id = $this->getUserid( $m_order );
        $data_so = array(
            "order_id" => $m_order->order_id,
            "old_status" => $m_order->pay_status_old,
            "new_status" => $m_order->pay_status,
            "notes" => $notes,
            "user_type" => $m_order->user_type,
            "user_id" => $user_id,
            "show4user" => $m_order->show4user
        );

        /// 记录_支付状态变动;
        $this->statusPayRecordRepository->create($data_so);
    }

    /**
     * 处理发货状态事件
     * @param $event
     * @throws \Exception
     * @author ^2_3^岑业宝
     */
    public function onShippingStatus($event) {
        /// 订单模型
        $m_order = $event->orderInfo;

        /// 验证参数
        if( !isset($m_order->shipping_status_old) ||
            !isset($m_order->user_type) ||
            !isset($m_order->show4user)
        ) {
            $msg_err = '[发货状态事件] 提示：缺少属性；';
            $msg_err .= '使用 model->field = attribute 方式设置以下属性; shipping_status_old(变动前发货状态值)；';
            $msg_err .= 'user_type(操作人员类型: 0-系统默认; 1-管理人员; 2-用户)；';
            $msg_err .= 'show4user(是否显示给用户, 1-显示, 0-不显示;)；';

            throw new \Exception($msg_err, 400);
        }

        /// 发货状态
        $a_s_status = config('custom.shipping_status');
        $notes = "{$a_s_status[$m_order->shipping_status_old]}({$m_order->shipping_status_old})";
        $notes .= " 改成 {$a_s_status[$m_order->shipping_status]}({$m_order->shipping_status});";

        /// 记录数据
        $user_id = $this->getUserid( $m_order );
        $data_so = array(
            "order_id" => $m_order->order_id,
            "old_status" => $m_order->shipping_status_old,
            "new_status" => $m_order->shipping_status,
            "notes" => $notes,
            "user_type" => $m_order->user_type,
            "user_id" => $user_id,
            "show4user" => $m_order->show4user
        );

        /// 记录_发货状态变动;
        $this->statusShippingRecordRepository->create($data_so);
    }

    /**
     * 获取用户ID
     * @param $m_order 订单模型对象;
     * @return int 用户ID;
     * @author  ^2_3^岑业宝
     */
    private function getUserid( $m_order ) {
        switch ($m_order->user_type) {
            case 0:
                //-/ 系统自动操作
                $user_id = 0;
                break;
            case 1:
                //-/ 管理人员操作
                /// TODO 获取管理人员ID;
                $user_id = 0;
                break;
            case 2:
                //-/ 用户触发操作
                $user_id = $m_order->user_id;
                break;
            default:
                $user_id = 0;
        }
        return $user_id;
    }

    /**
     * 为订阅者注册监听器
     *
     * @param  Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        /// 订单状态事件监听
        $events->listen(
            'App\Events\EventOrderStatus',
            'App\Listeners\EventOrderSubscriber@onOrderStatus'
        );

        /// 支付状态事件监听
        $events->listen(
            'App\Events\EventPayStatus',
            'App\Listeners\EventOrderSubscriber@onPayStatus'
        );

        /// 发货状态事件监听
        $events->listen(
            'App\Events\EventShippingStatus',
            'App\Listeners\EventOrderSubscriber@onShippingStatus'
        );

    }

}